﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 模块列表类。
	/// </summary>
	public class ModuleList : BaseRepeaterList
	{
		private LoadedValue<string> loadedModuleCode;
		/// <summary>
		/// 列表的模块代码。
		/// </summary>
		public virtual string ModuleCode
		{
			get
			{
				if (loadedModuleCode.Loader == null)
					loadedModuleCode.Loader = () => (string)ViewState["ModuleCode"];
				return loadedModuleCode.Value;
			}
			set
			{
				if (loadedModuleCode.Setter == null)
					loadedModuleCode.Setter = v => ViewState["ModuleCode"] = v;
				loadedModuleCode.Value = value;
			}
		}

		private LoadedValue<Module.Module> loadedModuleInfo;
		public Module.Module ModuleInfo
		{
			get
			{
				if (loadedModuleInfo.Loader == null)
					loadedModuleInfo.Loader = () => SiteId.HasValue ? Modules.GetItem(SiteId.Value, ModuleCode) : null;
				return loadedModuleInfo.Value;
			}
		}

		public int? ModuleId
		{
			get
			{
				if (ModuleInfo == null) return null;
				return ModuleInfo.ModuleId;
			}
		}

		private LoadedValue<string> loadedListTitle;
		/// <summary>
		/// 列表的标题（如果未设置则使用模块名称）。
		/// </summary>
		public override string ListTitle
		{
			get
			{
				if (loadedListTitle.Loader == null)
					loadedListTitle.Loader = () => (string)ViewState["ListTitle"];
				return string.IsNullOrEmpty(loadedListTitle.Value) ? Modules.GetNameById(SiteId, ModuleCode) : loadedListTitle.Value;
			}
			set
			{
				if (loadedListTitle.Setter == null)
					loadedListTitle.Setter = v => ViewState["ListTitle"] = v;
				loadedListTitle.Value = value;
			}
		}



		/// <summary>
		/// 列表标题字段名。
		/// </summary>
		public override string TitleField
		{
			get { return "ModuleName"; }
		}

		/// <summary>
		/// 列表明日期字段。
		/// </summary>
		public override string DateTimeField
		{
			get
			{
				return "CreationDate";
			}
		}

		/// <summary>
		/// 列表项的Url。
		/// </summary>
		public override string ItemUrl
		{
			get { return GenericUrlManager.BuildArticleListUrl(ItemModuleId, null); }
		}

		/// <summary>
		/// 列表项的模块ID。
		/// </summary>
		public virtual int ItemModuleId
		{
			get { return (int)Eval("ModuleId"); }
		}

		/// <summary>
		/// 列表项的模块代码。
		/// </summary>
		public virtual string ItemModuleCode
		{
			get { return Eval("ModuleCode", null); }
		}

		/// <summary>
		/// 获取列表数据。
		/// </summary>
		/// <returns>列表数据。</returns>
		public override object GetListData()
		{
			return Modules.GetChildren(SiteId, ModuleId);
		}
	}
}